package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.spc_report.SpcReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.ISpcReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;


@Slf4j
@Service
public class SpcReportServiceImpl implements ISpcReportService {


    public List<SpcReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("b_1.data_desc as toolg_desc, b_2.tool_dsc as tool_desc, A.*, B.*");
        sqlVo.appendSql("FROM icim_jn.spc_ret_sum_rpt_dura A");
        sqlVo.appendSql("INNER JOIN icim_jn.spc_ret_sum_rpt B ON A.sum_seq_id = B.sum_seq_id_fk");
        sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'TOLG') b_1 ON A.area_id_fk = b_1.data_ext");
        sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from icim_jn.bis_tool ) b_2 ON B.tool_id_fk = b_2.tool_id");
        if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo.appendSql("INNER JOIN (select tool_id from bis_tool where unit_typ='" + conditions.getTool_type() + "') E ON E.tool_id = B.tool_id_fk");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND (B.TOOL_ID_FK IN(select tool_id from icim_jn.bis_tool where fab_id_fk in" + jointInSql(conditions.getFab_id()) + ") OR B.TOOL_ID_FK = 'T')");
        if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo.appendSql("AND A.AREA_ID_FK in" + jointInSql(conditions.getToolg_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getCollect_type())) sqlVo.appendSql("AND A.COL_TYP_FK in" + jointInSql(conditions.getCollect_type()));
        if (StringUtils.isNotEmpty(conditions.getReport_type())) sqlVo.appendSql("AND A.RPT_TYP_FK in" + jointInSql(conditions.getReport_type()));
        if (StringUtils.isNotEmpty(conditions.getData_group())) sqlVo.appendSql("AND B.DATA_GROUP_FK in" + jointInSql(conditions.getData_group()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.RPT_B_DATE >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.RPT_B_DATE <= ?", conditions.getEnd_time());
        if (StringUtils.isNotEmpty(conditions.getControl_chart_grp())) {
            String grpNo_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[0]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String grpName_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[2]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            sqlVo.appendSql("AND B.grp_no_fk in" + grpNo_list_str);
            sqlVo.appendSql("AND B.grp_name_fk in" + grpName_list_str);
        }
        if (StringUtils.isNotEmpty(conditions.getControl_chart())) {
            String chartNo_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[0]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String chartTyp_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[1]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String chartName_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[2]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            sqlVo.appendSql("AND B.chart_no_fk in" + chartNo_list_str);
            sqlVo.appendSql("AND B.chart_typ_fk in" + chartTyp_list_str);
            sqlVo.appendSql("AND B.chart_name_fk in" + chartName_list_str);
        }
        sqlVo.appendSql("ORDER BY B.CALC_E_PROC_TIMESTAMP");

        return mySql_QueryRunner_QueryListBySql(sqlVo, SpcReportGridOut.class);
    }



}
